package com.zhihesj.anan.utils;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HutoolUtil {

    /**
     * 获取token
     * @param count
     * @return
     */
    public String getToken(Integer count){
        return RandomUtil.randomString(count);
    }

    public void test() throws FileNotFoundException {
        FileReader fileReader = new FileReader("application22.yml");
    }

    /**
     * 传入数组LIST，合并相同的值并返回
     * @param
     * @return
     */
    public List<Long> mergeList(List<Long> oldList) {
        List<Long> newList = new ArrayList<>();
        for (Long testObjOld : oldList) {
            // 遍历新的List，看是否存在，存在则count加一，不存在则放入新的List
            boolean flag = false;
            for (Long testObjNew : newList) {
                if (testObjNew.equals(testObjOld)) {
                    // 新的List中存在名字相同的，则count+1
                    flag = true;
                }
            }
            // 新的List中不存在
            if (!flag) {
                newList.add(testObjOld);
            }
        }
        return newList;
    }

    /**
     * 权限字符串 例：目录："page",链接："/permission"
     * 批量删除权限
     * @param
     * @param roles 需要给予的权限
     */
    public void deleteRole(String roles){
        String routerJs = "E:\\project\\vue\\element-admin\\src\\router\\index.js";
        FileReader fileReader = new FileReader(routerJs);
        List<String> list = fileReader.readLines();
        // 获取路径行数
        Map<String, Integer> map1 = new HashMap<>();
        Map<String,Integer> map = getPathNum(list, 0, 0,"roles: [");
        while (map.get("isYes") == 1) {
            map1 = getPathNum(list, map.get("pathNum"), 6,roles);
            if (map1.get("isYes") == 1) {
                String str=list.get(map1.get("pathNum"));
                if (str.indexOf("'" + roles + "', ") > 0) {
                    str = StringUtils.remove(str, "'" + roles + "', ");
                } else if (str.indexOf(", '" + roles + "'") > 0) {
                    str = StringUtils.remove(str, ", '" + roles + "'");
                } else if (str.indexOf("'" + roles + "'") > 0) {
                    str = StringUtils.remove(str, "'" + roles + "'");
                }
                list.set(map1.get("pathNum"), str);
            }
            map = getPathNum(list, map1.get("pathNum")+1,0,"roles: [");
        }
//        FileWriter fileWriter = new FileWriter("D:\\360安全浏览器下载\\考试工具\\测试文件.txt");
        FileWriter fileWriter = new FileWriter("E:\\project\\vue\\element-admin\\src\\router\\index.js");
        fileWriter.writeLines(list);
    }

    /**
     * 设置权限
     * @param path 权限字符串 例：目录："page",链接："/permission"
     * @param roles 需要给予的权限
     */
    public void setRole(String path,String roles){
        String contents = "path: '" + path + "',";
        String routerJs = "E:\\project\\vue\\element-admin\\src\\router\\index.js";
        FileReader fileReader = new FileReader(routerJs);
        List<String> list= fileReader.readLines();
        // 获取路径行数
        Map<String,Integer> map = getPathNum(list,0 ,6,contents);
        Map<String,Integer> map1 = getPathNum(list, map.get("pathNum"),6, "meta: {");
        Map<String,Integer> map2 = getPathNum(list, map1.get("pathNum"),6, "roles: [");

        if (map2.get("isYes") == 1) {//有roles字段
            if (list.get(map2.get("pathNum")).indexOf(roles)==-1) {
                StringBuilder sb = new StringBuilder(list.get(map2.get("pathNum")));
                sb.insert(list.get(map2.get("pathNum")).indexOf("]"), ", '" + roles + "'");
                list.set(map2.get("pathNum"), sb.toString());
            }
        }else {//没有roles字段，但是有meta字段
            if (list.get(map1.get("pathNum")).indexOf(roles)==-1) {
                String role="";
                Integer integer=list.get(map1.get("pathNum")).indexOf("meta:");
                for (int i = 0; i < integer+2; i++) {
                    role += " ";
                }
                role += "roles: ['" + roles + "'],";
                list.add(map1.get("pathNum")+1, role);//插入一行
            }
        }
//        FileUtil.touch("D:\\360安全浏览器下载\\考试工具\\测试文件.txt");
//        FileWriter fileWriter = new FileWriter("D:\\360安全浏览器下载\\考试工具\\测试文件.txt");
        FileWriter fileWriter = new FileWriter("E:\\project\\vue\\element-admin\\src\\router\\index.js");
        fileWriter.writeLines(list);
    }

    /**
     * 获取字符串所在的行数
     * @param list  list
     * @param num   从num行开始
     * @param contents  需要查询的字符串
     * @return
     */
    private Map<String, Integer> getPathNum(List<String> list, Integer num, Integer length,String contents) {
        Map<String, Integer> map = new HashMap<>();
        Integer pathNum=num;
        Integer isYes = 0;
        if (list.size() > 0) {
            if (num == 0) {
                for (int i = num; i < list.size(); i++) {
                    if (list.get(i).indexOf(contents) > 0) {
                        pathNum = i;
                        isYes = 1;
                        break;
                    }
                }
            } else {
                for (int i = num; i < (length == 0 ? list.size() : num + length); i++) {
                    if (list.get(i).indexOf(contents) > 0) {
                        pathNum = i;
                        isYes = 1;
                        break;
                    }
                }
            }
        }
        map.put("pathNum", pathNum);
        map.put("isYes", isYes);
        return map;
    }






}
